package com.iplant.aps.service.po.mrp;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.base.utils.annotation.condition.DBCondition;
import com.iplant.base.utils.annotation.join.DBJoin;
import com.iplant.base.utils.annotation.join.multi.select.DBJoinMultiSelect;
import com.iplant.base.utils.annotation.po.DBBasePO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 物料需求计划
 *
 * @author YouWang·Peng
 * @CreateTime 2022-3-8 11:02:07
 */

@DBBasePO(TableName = "mrp_materialplan", Schemas = DBSchemas.APS, Name = false, Active = false,
        CheckName = {"Code", "OrderID,MaterialID,DemandMonth"},
        CodeAuto = true, CodePrefix = "MRP-{yyMMdd}-")
public class MRPMaterialPlan extends BasePO implements Serializable {
    /**
     * 序列号
     */
    private static final long serialVersionUID = 1L;


    /**
     * 工厂ID
     */
    @DBCondition
    @DBField
    public int FactoryID = 0;
    /**
     * 工厂名称
     */
    @DBJoin(name = "Name", tableName = "fmc_factory", tableAsName = "fmc_factory1", Schemas = DBSchemas.Basic,
            MainTableField = "FactoryID")
    public String FactoryName = "";
    /**
     * 工厂编码
     */
    @DBJoin(name = "Code", tableName = "fmc_factory", tableAsName = "fmc_factory1", Schemas = DBSchemas.Basic,
            MainTableField = "FactoryID")
    public String FactoryCode = "";

    /**
     * 车型ID
     */
    @DBCondition
    @DBField(CheckValue = true)
    public int ProductID = 0;
    @DBJoin(name = "Code", tableName = "fpc_product", tableAsName = "fpc_product1", Schemas = DBSchemas.PDM,
            MainTableField = "ProductID")
    public String ProductCode = "";

    @DBJoin(name = "Name", tableName = "fpc_product", tableAsName = "fpc_product1", Schemas = DBSchemas.PDM,
            MainTableField = "ProductID")
    public String ProductName = "";

    @DBCondition
    @DBField(CheckValue = true)
    public int LineID = 0;

    @DBJoin(name = "Name", tableName = "fmc_line", tableAsName = "l1", Schemas = DBSchemas.PDM,
            MainTableField = "LineID")
    public String LineName = "";

    @DBJoin(name = "Code", tableName = "fmc_line", tableAsName = "l1", Schemas = DBSchemas.PDM,
            MainTableField = "LineID")
    public String LineCode = "";

    @DBCondition
    @DBField
    public int CustomerID = 0;
    /**
     * 客户名称
     */
    @DBJoin(name = "Name", tableName = "crm_customer", tableAsName = "crm_customer1", Schemas = DBSchemas.Basic,
            MainTableField = "CustomerID")
    public String CustomerName = "";
    /**
     * 客户编码
     */
    @DBJoin(name = "Code", tableName = "crm_customer", tableAsName = "crm_customer1", Schemas = DBSchemas.Basic,
            MainTableField = "CustomerID")
    public String CustomerCode = "";


    /**
     * 订单ID
     */
    @DBCondition
    @DBField
    public int OrderID = 0;

    @DBCondition
    @DBField
    public String OrderNo = "";


    /**
     * 工件号（广机：车号）
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "PartNo", tableName = "oms_order", tableAsName = "oms1", Schemas = DBSchemas.APS,
            MainTableField = "OrderID")
    public String PartNo = "";


    /**
     * 项目号
     */
    @DBCondition
    @DBJoin(name = "WBSNo", tableName = "oms_order", tableAsName = "oms1", Schemas = DBSchemas.APS,
            MainTableField = "OrderID")
    public String WBSNo = "";
    /**
     * 物料ID
     */
    @DBCondition
    @DBField(CheckValue = true)
    public int MaterialID = 0;
    /**
     * 物料编码
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Code", tableName = "mss_material", tableAsName = "m2", Schemas = DBSchemas.Basic,
            MainTableField = "MaterialID")
    public String MaterialCode = "";

    /**
     * 物料名称
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "MaterialName", tableName = "mss_material", tableAsName = "m2", Schemas = DBSchemas.Basic,
            MainTableField = "MaterialID")
    public String MaterialName = "";
    /**
     * 物料类型ID
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "MaterialTypeID", tableName = "mss_material", tableAsName = "m2", Schemas = DBSchemas.Basic,
            MainTableField = "MaterialID")
    public int MaterialTypeID = 0;


    @DBCondition
    @DBJoin(name = "MaterialGroupID", tableName = "mss_material", tableAsName = "m2", JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, MainTableField = "MaterialID")
    public int MaterialGroupID = 0;

    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Code", tableName = "mss_materialgroup", tableAsName = "m1",
            JoinIndex = 3, JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, ConditionalJoin = "m2.MaterialGroupID=m1.ID")
    public String MaterialGroupCode = "";

    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Name", tableName = "mss_materialgroup", tableAsName = "m1",
            JoinIndex = 3, JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, ConditionalJoin = "m2.MaterialGroupID=m1.ID")
    public String MaterialGroupName = "";


    @DBCondition(CompareType = DBCondition.DBCompareType.leftLike)
    @DBJoin(name = "FullGroupName", tableName = "mss_materialgroup", tableAsName = "m1",
            JoinIndex = 3, JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, ConditionalJoin = "m2.MaterialGroupID=m1.ID")
    public String MaterialFullGroup = "";

    /**
     * 物料类型名称
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Name", tableName = "mss_materialtype", tableAsName = "mt1",
            JoinIndex = 4, JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, ConditionalJoin = "m2.MaterialTypeID=mt1.ID")
    public String MaterialTypeName = "";

    /**
     * 物料类型名称
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Name", tableName = "mss_materialtype", tableAsName = "mt1",
            JoinIndex = 4, JoinMode = DBJoin.DBJoinMode.Inner,
            Schemas = DBSchemas.Basic, ConditionalJoin = "m2.MaterialTypeID=mt1.ID")
    public String MaterialTypeCode = "";
    /**
     * 需求数量
     */
    @DBField
    public double PlanFQTY = 0.0;



    /**
     * 实际配料数量
     */
    @DBField
    public double FQTYDone = 0.0;

    /**
     * 需求月份
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.geLt, ParamName = "StartDate", ParamName2 = "EndDate")
    @DBField
    public Calendar DemandMonth = Calendar.getInstance();

    @DBJoinMultiSelect(OutTableField = "PlanID", ItemClazz = MRPMaterialItem.class)
    public List<MRPMaterialItem> ItemList = new ArrayList<>();

    public MRPMaterialPlan() {

        super();
    }


    public void setMaterialPlanItem(List<MRPMaterialItem> wMaterialItemList){

        PlanFQTY = wMaterialItemList.stream().mapToDouble(p -> p.PlanFQTY).sum();

        if(ItemList==null||ItemList.size()==0){
            ItemList = wMaterialItemList;
            return;
        }
        for (MRPMaterialItem mrpDeliveryItem : wMaterialItemList) {

            mrpDeliveryItem.PlanID=ID;
            for (MRPMaterialItem deliveryItem : ItemList) {
                if(mrpDeliveryItem.OrderID!=deliveryItem.OrderID
                        ||mrpDeliveryItem.MaterialID!=deliveryItem.MaterialID
                        ||mrpDeliveryItem.RoutePartID!=deliveryItem.RoutePartID
                        ||mrpDeliveryItem.RoutePartPointID!=deliveryItem.RoutePartPointID
                        ||mrpDeliveryItem.DemandDate.compareTo(deliveryItem.DemandDate)!=0)
                    continue;

                mrpDeliveryItem.ID=deliveryItem.ID;
            }
        }
        ItemList = wMaterialItemList;
    }
    public MRPMaterialPlan(OMSOrder wOMSOrder, Calendar wDemandMonth,
                           List<MRPMaterialItem> wMaterialItemList) {
        super();
        DemandMonth = wDemandMonth;

        FactoryID = wOMSOrder.FactoryID;
        FactoryName = wOMSOrder.FactoryName;
        FactoryCode = wOMSOrder.FactoryCode;
        ProductID = wOMSOrder.ProductID;
        ProductCode = wOMSOrder.ProductCode;
        ProductName = wOMSOrder.ProductName;
        LineID = wOMSOrder.LineID;
        LineName = wOMSOrder.LineName;
        LineCode = wOMSOrder.LineCode;
        CustomerID = wOMSOrder.CustomerID;
        CustomerName = wOMSOrder.CustomerName;
        CustomerCode = wOMSOrder.CustomerCode;
        OrderID = wOMSOrder.ID;
        OrderNo = wOMSOrder.Code;
        PartNo = wOMSOrder.PartNo;

        MaterialID = wMaterialItemList.get(0).MaterialID;
        MaterialName = wMaterialItemList.get(0).MaterialName;
        MaterialCode = wMaterialItemList.get(0).MaterialCode;
        MaterialTypeID = wMaterialItemList.get(0).MaterialTypeID;
        MaterialTypeName = wMaterialItemList.get(0).MaterialTypeName;
        MaterialTypeCode = wMaterialItemList.get(0).MaterialTypeCode;
        MaterialGroupID = wMaterialItemList.get(0).MaterialGroupID;
        MaterialGroupCode = wMaterialItemList.get(0).MaterialGroupCode;
        MaterialGroupName = wMaterialItemList.get(0).MaterialGroupName;
        MaterialFullGroup = wMaterialItemList.get(0).MaterialFullGroup;

        PlanFQTY = wMaterialItemList.stream().mapToDouble(p -> p.PlanFQTY).sum();


        ItemList = wMaterialItemList;
    }


    @JsonProperty("FactoryID")
    public int getFactoryID() {
        return FactoryID;
    }

    public void setFactoryID(int factoryID) {
        FactoryID = factoryID;
    }

    @JsonProperty("FactoryName")
    public String getFactoryName() {
        return FactoryName;
    }

    public void setFactoryName(String factoryName) {
        FactoryName = factoryName;
    }

    @JsonProperty("FactoryCode")
    public String getFactoryCode() {
        return FactoryCode;
    }

    public void setFactoryCode(String factoryCode) {
        FactoryCode = factoryCode;
    }


    @JsonProperty("MaterialID")
    public int getMaterialID() {
        return MaterialID;
    }

    public void setMaterialID(int materialID) {
        MaterialID = materialID;
    }

    @JsonProperty("MaterialCode")
    public String getMaterialCode() {
        return MaterialCode;
    }

    public void setMaterialCode(String materialCode) {
        MaterialCode = materialCode;
    }

    @JsonProperty("MaterialName")
    public String getMaterialName() {
        return MaterialName;
    }

    public void setMaterialName(String materialName) {
        MaterialName = materialName;
    }

    @JsonProperty("MaterialTypeID")
    public int getMaterialTypeID() {
        return MaterialTypeID;
    }

    public void setMaterialTypeID(int materialTypeID) {
        MaterialTypeID = materialTypeID;
    }

    @JsonProperty("MaterialGroupID")
    public int getMaterialGroupID() {
        return MaterialGroupID;
    }

    public void setMaterialGroupID(int materialGroupID) {
        MaterialGroupID = materialGroupID;
    }

    @JsonProperty("MaterialGroupCode")
    public String getMaterialGroupCode() {
        return MaterialGroupCode;
    }

    public void setMaterialGroupCode(String materialGroupCode) {
        MaterialGroupCode = materialGroupCode;
    }

    @JsonProperty("MaterialGroupName")
    public String getMaterialGroupName() {
        return MaterialGroupName;
    }

    public void setMaterialGroupName(String materialGroupName) {
        MaterialGroupName = materialGroupName;
    }

    @JsonProperty("MaterialFullGroup")
    public String getMaterialFullGroup() {
        return MaterialFullGroup;
    }

    public void setMaterialFullGroup(String materialFullGroup) {
        MaterialFullGroup = materialFullGroup;
    }

    @JsonProperty("MaterialTypeName")
    public String getMaterialTypeName() {
        return MaterialTypeName;
    }

    public void setMaterialTypeName(String materialTypeName) {
        MaterialTypeName = materialTypeName;
    }

    @JsonProperty("MaterialTypeCode")
    public String getMaterialTypeCode() {
        return MaterialTypeCode;
    }

    public void setMaterialTypeCode(String materialTypeCode) {
        MaterialTypeCode = materialTypeCode;
    }


    @JsonProperty("ProductID")
    public int getProductID() {
        return ProductID;
    }

    public void setProductID(int productID) {
        ProductID = productID;
    }

    @JsonProperty("ProductCode")
    public String getProductCode() {
        return ProductCode;
    }

    public void setProductCode(String productCode) {
        ProductCode = productCode;
    }

    @JsonProperty("ProductName")
    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;
    }

    @JsonProperty("LineID")
    public int getLineID() {
        return LineID;
    }

    public void setLineID(int lineID) {
        LineID = lineID;
    }

    @JsonProperty("LineName")
    public String getLineName() {
        return LineName;
    }

    public void setLineName(String lineName) {
        LineName = lineName;
    }

    @JsonProperty("LineCode")
    public String getLineCode() {
        return LineCode;
    }

    public void setLineCode(String lineCode) {
        LineCode = lineCode;
    }

    @JsonProperty("CustomerID")
    public int getCustomerID() {
        return CustomerID;
    }

    public void setCustomerID(int customerID) {
        CustomerID = customerID;
    }

    @JsonProperty("CustomerName")
    public String getCustomerName() {
        return CustomerName;
    }

    public void setCustomerName(String customerName) {
        CustomerName = customerName;
    }

    @JsonProperty("CustomerCode")
    public String getCustomerCode() {
        return CustomerCode;
    }

    public void setCustomerCode(String customerCode) {
        CustomerCode = customerCode;
    }

    @JsonProperty("OrderID")
    public int getOrderID() {
        return OrderID;
    }

    public void setOrderID(int orderID) {
        OrderID = orderID;
    }

    @JsonProperty("OrderNo")
    public String getOrderNo() {
        return OrderNo;
    }

    public void setOrderNo(String orderNo) {
        OrderNo = orderNo;
    }

    @JsonProperty("PartNo")
    public String getPartNo() {
        return PartNo;
    }

    public void setPartNo(String partNo) {
        PartNo = partNo;
    }

    @JsonProperty("PlanFQTY")
    public double getPlanFQTY() {
        return PlanFQTY;
    }

    public void setPlanFQTY(double planFQTY) {
        PlanFQTY = planFQTY;
    }

    @JsonProperty("ActFQTY")
    public double getFQTYDone() {
        return FQTYDone;
    }

    public void setFQTYDone(double FQTYDone) {
        this.FQTYDone = FQTYDone;
    }

    @JsonProperty("DemandMonth")
    public Calendar getDemandMonth() {
        return DemandMonth;
    }

    public void setDemandMonth(Calendar demandMonth) {
        DemandMonth = demandMonth;
    }

    @JsonProperty("WBSNo")
    public String getWBSNo() {
        return WBSNo;
    }

    public void setWBSNo(String WBSNo) {
        this.WBSNo = WBSNo;
    }


    @JsonProperty("ItemList")
    public List<MRPMaterialItem> getItemList() {
        return ItemList;
    }

    public void setItemList(List<MRPMaterialItem> itemList) {
        ItemList = itemList;
    }
}
